using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Camera))]
public class CameraZoom : MonoBehaviour
{
    public float maxDistance = 10;
    public float minDistance = 2;
    public float lastDist = 0f;
    public float curDist = 0f;
    [Header("俯角(0-89)")]
    public float maxDepression = 80;
    [Header("仰角(0-89)")]
    public float maxElevation = 80;
    public float hideTargetAngle = 75;
    public float distanceSpeed = 0.1f;
    public float rotateXSpeed = 0.02f;
    public float rotateYSpeed = 0.05f;
    public float minScaleDistance = 1f;
    public float minRotateDistance = 15f;
    public float leftAreaWidth = 420;
    public CameraFollow cf;
    public bool isMovingCamera = false;
    public bool showDebugGui = false;
    private Camera mainCamera;
    private float offsetYSensitivity;
    private float xAngleSensitivity;
    private float distanceSensitivity;
    private float distanceVec = 0;
    private float curDistance = 0;
    private float distance = -1;
    //手动添加x转向
    private float addRotateDist = 0f;
    private float rotateDist = 0;
    private float vertDist = 0;
    private float _strength = 5;
    private bool[] isPointOverUI = new bool[3];
    private Vector2[] touchBeginPos = new Vector2[3];

    public void Init()
    {
        if (mainCamera == null)
            mainCamera = GetComponent<Camera>();

        distance = -1;
        distanceVec = 0;
        curDistance = 0;

        addRotateDist = 0f;
        rotateDist = 0;
        vertDist = 0;

        distanceSensitivity = maxDistance - minDistance;
        ResetPointVerUI();
    }

    void ResetPointVerUI()
    {
        for (int i = 0; i < isPointOverUI.Length; i++)
        {
            isPointOverUI[i] = false;
            touchBeginPos[i] = Vector2.zero;
        }
    }

    void Update()
    {
        if (mainCamera == null) return;
        if (GameUtil.isMobilePlatform)
        {
            if (Input.touchCount > 0)
            {
                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (i >= 3) break;
                    if (Input.GetTouch(i).phase == TouchPhase.Began)
                    {
                        touchBeginPos[i] = Input.GetTouch(i).position;
                        isPointOverUI[i] = IsPointerOverGameObject(touchBeginPos[i]);
                    }
                    else if (Input.GetTouch(i).phase == TouchPhase.Ended)
                    {
                        isPointOverUI[i] = false;
                        touchBeginPos[i] = Vector2.zero;
                    }

                }
            }

            if (Input.touchCount <= 0)
            {
                ResetPointVerUI();
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(1))
            {
                isPointOverUI[0] = IsPointerOverGameObject(Input.mousePosition);
            }
            else if (Input.GetMouseButtonUp(1))
            {
                isPointOverUI[0] = false;
            }
        }

    }

    private bool IsPointerOverGameObject(Vector2 pointerPosition)
    {
        if (pointerPosition.x < leftAreaWidth)
        {
            return true;
        }

        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = pointerPosition;
        List<RaycastResult> result = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, result);
        return result.Count > 0;
    }

    public void ResetDistance(float maxDis)
    {
        distanceSensitivity = maxDis - minDistance;
    }

//     private bool CameraScale()
//     {
// #if UNITY_ANDROID || UNITY_IOS
// #endif
//     }

}
